package web.action;

import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Product;
import model.ProductQuery;
import model.Shop;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import service.ProManageService;
import util.Pager;

public class ProManageAction {
	Logger logger = Logger.getLogger(this.getClass());
	private ProManageService proManageService;
	private Pager pager;
	private ProductQuery query;
	public String init(){
		
		return "search";
	}
	public String showProducts(){
		List<Product> list = null;
		boolean re = false;
		try {
			list = this.proManageService.getPros(query);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		ActionContext ctx = ActionContext.getContext();
		ctx.put("list", list);
		ctx.put("query", query);
		return "list";
	}
	
	public void getProducts(){
		List<Product> list = null;
		boolean re = false;
		try {
			list = this.proManageService.getPros(query);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			JSONObject json = new JSONObject();
			JSONArray js = JSONArray.fromObject(list);
			out.write(js.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	public ProManageService getProManageService() {
		return proManageService;
	}
	public void setProManageService(ProManageService proManageService) {
		this.proManageService = proManageService;
	}
	public Pager getPager() {
		return pager;
	}
	public void setPager(Pager pager) {
		this.pager = pager;
	}
	public ProductQuery getQuery() {
		return query;
	}
	public void setQuery(ProductQuery query) {
		this.query = query;
	}
	
}
